<?php
namespace Home\Controller;
use Think\Controller;
/**
 * 发布评论必须登录
 */
class UserReviewController extends Controller{

    public function index($name){
        $this->display();
    }
    //显示列表
    public function view($id,$status=0){
        if($status != 2){
            $list = M('userReview')->field("username,introduction")->join("left join user on user_review.user_id=user.id")->where("user_review.flag=1 and project_id=".$id)->limit("2")->select();
        }else{
    	    $list = M('userReview')->field("username,introduction")->join("left join user on user_review.user_id=user.id")->where("user_review.flag=1 and project_id=".$id)->select();
        }
        //$post = M('post')->find($id);
        $this->assign('list', $list);
        $this->display();
    }
    //显示列表ajax方式
    public function ajaxview(){
        $id = $_GET["id"];
        $project = M("project")->where("id=%d",array($id))->find();
        $status = $_GET["status"];
        if($status != 2){
            $list = M('userReview')->field("insert(username, 4, 4, '****') as username,introduction,from_unixtime(addtime) as time")->join("left join `user` on user_review.user_id=`user`.id")->where("user_review.flag=1 and project_id=".$id)->limit("0,2")->order("addtime desc")->select();
            echo json_encode($list);
            // echo M()->getLastSql();exit;
        }else{
            $p = intval($_GET["p"]);
            $list = M('userReview')->field("insert(username, 4, 4, '****') as username,introduction,from_unixtime(addtime) as time")->join("left join `user` on user_review.user_id=`user`.id")->where("user_review.flag=1 and project_id=".$id)->order("addtime desc")->select();
            $data["num"] = ceil(count($list)/4);
            $data["name"]= $project["name"];
            $data["list"] = M('userReview')->field("insert(username, 4, 4, '****') as username,introduction,from_unixtime(addtime) as time")->join("left join `user` on user_review.user_id=`user`.id")->where("user_review.flag=1 and project_id=".$id)->order("addtime desc")->limit((($p-1)*4).",4")->select();
            echo json_encode($data);
        }
        // echo M()->getLastSql();exit;
        //$post = M('post')->find($id);
    }
    //增加评论(post:user_id,project_id,introduction)
    public function addReview(){
        // var_dump($_POST);exit;
        $data["user_id"] = $_POST["user_id"];
        $data["project_id"] = $_POST["project_id"];
        $data["introduction"] = $_POST["introduction"];
        $data["addtime"] = time();
        $data["flag"] = 1;
        M()->table("user_review")->add($data);
        echo json_encode("提交成功");
    	/*$model = D("UserReview");
    	if (!$model->create()) {
            // 如果创建失败 表示验证没有通过 输出错误提示信息
            echo json_encode("提交失败1");
            exit;
        } else {
            // $model->user_id = $uid;
            $model->flag = 1;
            if ($model->add()) {
                echo json_encode("提交成功");
            } else {
                echo json_encode("提交失败2");
            }
        }*/
    }
	
	
	
	
    //显示列表ajax方式
    public function ajaxviewsp(){
        $id = $_GET["id"];
        $project = M("post")->where("id=%d",array($id))->find();
        $status = $_GET["status"];
        if($status != 2){
            $list =M()->table("user_reviewsp")->field("insert(username, 4, 4, '****') as username,introduction,from_unixtime(addtime) as time")->join("left join `user` on user_reviewsp.user_id=`user`.id")->where("user_reviewsp.flag=1 and project_id=".$id)->limit("0,2")->order("addtime desc")->select();

            echo json_encode($list);
            // echo M()->getLastSql();exit;
        }else{
            $p = intval($_GET["p"]);
            $list =M()->table("user_reviewsp")->field("insert(username, 4, 4, '****') as username,introduction,from_unixtime(addtime) as time")->join("left join `user` on user_reviewsp.user_id=`user`.id")->where("user_reviewsp.flag=1 and project_id=".$id)->order("addtime desc")->select();
            $data["num"] = ceil(count($list)/6);
            $data["name"]= $project["name"];
			$a=11;
            $data["list"] =M()->table("user_reviewsp")->field("insert(username, 4, 4, '****') as username,introduction,from_unixtime(addtime) as time")->join("left join `user` on user_reviewsp.user_id=`user`.id")->where("user_reviewsp.flag=1 and project_id=".$id)->order("addtime desc")->limit((($p-1)*6).",6")->select();
			
            echo json_encode($data);
        }
        // echo M()->getLastSql();exit;
        //$post = M('post')->find($id);
    }
    //增加评论(post:user_id,project_id,introduction)
    public function addReviewsp(){
        // var_dump($_POST);exit;
        $data["user_id"] = $_POST["user_id"];
        $data["project_id"] = $_POST["project_id"];
        $data["introduction"] = $_POST["introduction"];
        $data["addtime"] = time();
        $data["flag"] = 1;
        M()->table("user_reviewsp")->add($data);
        echo json_encode("提交成功");
    	/*$model = D("UserReview");
    	if (!$model->create()) {
            // 如果创建失败 表示验证没有通过 输出错误提示信息
            echo json_encode("提交失败1");
            exit;
        } else {
            // $model->user_id = $uid;
            $model->flag = 1;
            if ($model->add()) {
                echo json_encode("提交成功");
            } else {
                echo json_encode("提交失败2");
            }
        }*/
    }
	
}